package db

import (
	"WebApplication/model"
	"fmt"
)

// 增加
func (orderdatabase *OrderDatabase) Save(order *model.Order) OperatorResult{
	var operatorResult OperatorResult
	err := orderdatabase.Database.Save(order).Error
	// 操作失败了
	if err != nil{
		operatorResult.Error = err
		return operatorResult
	}
	// 没失败 返回operatorResult是没失败
	operatorResult.Result = order
	return operatorResult
}


// 查询所有结果
func (orderdatabase *OrderDatabase) FindAll() OperatorResult{
	var operatorResult OperatorResult
	var order []model.Order
	// 调用gorm封装的find函数，功能是查找
	operatorResult.Error = orderdatabase.Database.Find(&order).Error
	// 如果查找错误了 返回查找错误的OperatorResult
	if operatorResult.Error != nil{
		return operatorResult
	}
	// 如果查找成功  要把数据赋值给Result
	operatorResult.Result = order
	return operatorResult


}

func (orderdatabase *OrderDatabase) UpdateUrl(OrderNo string, fileurl string) OperatorResult{
	// 更新数据库
	var order model.Order
	var opResult OperatorResult
	fmt.Println(OrderNo)
	fmt.Println(fileurl)
	err := orderdatabase.Transaction.Model(&order).Where("order_no=?",OrderNo).Update("file_url", fileurl).Error
	if err != nil{
		opResult.Error = err
		return opResult
	}
	opResult.Result = model.Order{OrderNo:OrderNo, FileUrl:fileurl}
	return opResult
}

// 实现模糊查找 列表显示
func (orderdatabase *OrderDatabase)SelectAtName(name string) OperatorResult{
	var opResult OperatorResult
	var order model.Order
	opResult.Error = orderdatabase.Database.Model(&order).Where("user_name LIKE?", "%" + name + "%").Find(&order).Error
	if opResult.Error != nil{
		return opResult
	}
	opResult.Result = order
	return opResult
}